// 引入vue
import Vue from 'vue'
// 引入vuex
import Vuex from 'vuex'
// 使用vuex
Vue.use(Vuex)

const state = {
    bao: 'bao__--',
    // storesCart: [], // 商家缓存

    storesCart:  uni.getStorageSync('storesCart') || [], // 商家缓存
    userCart: uni.getStorageSync('userCart') || [], // 订购商缓存
}

const mutations = {
    addCart_x(state, payload) { // 添加 和 删除商品
        let LoginType = uni.getStorageSync('loginData').LoginType
        state[LoginType+'Cart'] = payload
        uni.setStorageSync(`${LoginType}Cart`, payload); // 缓存到本地
        console.log('== vuex ',  state.storesCart)
    },
    deleteCartItem_x(state, payload) {

    }

}

const actions = {

}

const getters = {
    // 返回 购物车 是属于什么类型的
    cartList_Gx(state) {
        let LoginType = uni.getStorageSync('loginData').LoginType
        if (LoginType == 'stores') {
            return state.storesCart
        } else if (LoginType == 'user') {
            return state.userCart
        }

    }
}

export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})
